﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;

namespace Cap_BG.Mvc.Controllers
{
    public class HomeController : Controller
    {

        private const string UrlBaseServ = "http://localhost:4252/ServCapBG.svc";

        public ActionResult Index()
        {
            ViewBag.Resultado = "";
            return View();
        }

        [HttpPost]
        public ActionResult Index(string txtValor1)
        {
            string result = string.Empty;
            using (var oHttp = new HttpClient())
            {
                //string uri = String.Format("{0}/Saludo/{1}", UrlBaseServ, txtValor1);//concatenado con el valor
                string uri = String.Format("{0}/SaludoP", UrlBaseServ);
                //HttpResponseMessage response = oHttp.GetAsync(uri).Result;
                ObjEnvio nOb = new ObjEnvio { dato = txtValor1 };
                HttpResponseMessage response = oHttp.PostAsJsonAsync(uri, nOb).Result;
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;

                    //List<Persona> convert = JsonConvert.DeserializeObject<List<Persona>>(result);

                }
                else
                {
                    result = "Error al llamar metodo.";
                }
            }
            ViewBag.Resultado = result;
            return View();
        }

        [HttpGet]
        public ActionResult LlamadaSaludo(string valor)
        {
            string result = string.Empty;
            using (var oHttp = new HttpClient())
            {
                string uri = String.Format("{0}/Saludo/{1}", UrlBaseServ, valor);//concatenado con el valor

                HttpResponseMessage response = oHttp.GetAsync(uri).Result;
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
                else
                {
                    result = "Error al llamar metodo.";
                }
            }
            return Json(new { Result = "OK", Msj = result }, JsonRequestBehavior.AllowGet);
        }

    }

    public class Persona
    {
        public string Nombre { get; set; }
        public string Apellidos { get; set; }
        public string Documento { get; set; }
    }

    public class ObjEnvio
    {
        public string dato { get; set; }
    }
}
